package io.renren.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.fabric.xmlrpc.base.Data;

import org.springframework.stereotype.Controller;

import io.renren.entity.ZjBranchAmountEntity;
import io.renren.service.ZjBranchAmountService;
import io.renren.utils.PageUtils;
import io.renren.utils.R;


/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-03-18 11:40:55
 */
@Controller
@RequestMapping("zjbranchamount")
public class ZjBranchAmountController {
	@Autowired
	private ZjBranchAmountService zjBranchAmountService;
	
	@RequestMapping("/zjbranchamount.html")
	public String list(){
		return "zjbranchamount/zjbranchamount.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("zjbranchamount:list")
	public R list(Integer page, Integer limit,String calTime,String value){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		if (calTime==null) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");  
			SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
			SimpleDateFormat formatMoneth = new SimpleDateFormat("MM");
			Calendar c = Calendar.getInstance();  
			c.add(Calendar.MONTH, -2); 
			String AllTime = format.format(c.getTime());
			String AllTime2=AllTime+"%";
			String Year = formatYear.format(c.getTime());
			int YesrInt=Integer.parseInt(Year);
			
			String Moneth = formatMoneth.format(c.getTime());
			int MonethInt=Integer.parseInt(Moneth);
			
			map.put("AllTime2", AllTime2);
			map.put("Year", YesrInt);
			map.put("Moneth", MonethInt);
			map.put("AllTime3", AllTime2);
			map.put("Year2", YesrInt);
			map.put("Moneth2", MonethInt);
		}else {
			String year = calTime.substring(0, 4);
			int YesrInt=Integer.parseInt(year);
			String moneth = calTime.substring(5, 7);
			int MonethInt=Integer.parseInt(moneth);
			String calTime2=calTime+"%";
			map.put("AllTime2", calTime2);
			map.put("Year", YesrInt);
			map.put("Moneth", MonethInt);
			map.put("AllTime3", calTime2);
			map.put("Year2", YesrInt);
			map.put("Moneth2", MonethInt);
			
		}
		
	
		//查询列表数据
		List<ZjBranchAmountEntity> zjBranchAmountList = zjBranchAmountService.queryList(map);
		int total = zjBranchAmountService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(zjBranchAmountList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("zjbranchamount:info")
	public R info(@PathVariable("id") Integer id){
		ZjBranchAmountEntity zjBranchAmount = zjBranchAmountService.queryObject(id);
		
		return R.ok().put("zjBranchAmount", zjBranchAmount);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("zjbranchamount:save")
	public R save(@RequestBody ZjBranchAmountEntity zjBranchAmount){
		zjBranchAmountService.save(zjBranchAmount);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("zjbranchamount:update")
	public R update(@RequestBody ZjBranchAmountEntity zjBranchAmount){
		zjBranchAmountService.update(zjBranchAmount);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("zjbranchamount:delete")
	public R delete(@RequestBody Integer[] ids){
		zjBranchAmountService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
